
using System;
using System.Linq;
using Csla;
using Csla.Data;
using HousingInfo.DataLinQ;

namespace HousingInfo.Library
{ 
	public partial class ForumNewsBOList : Csla.BusinessListBase<ForumNewsBOList, ForumNewsBO>
	{
        #region ham lay tat ca danh sach tin dien dan
        public static ForumNewsBOList GetAllForumNewsBOList()
        {
            return DataPortal.Fetch<ForumNewsBOList>();
        }
        #endregion
        #region Ham lay ID tu bang danh muc
        public static ForumNewsBOList GetForumNews_ByID(int id)
        { 
            return DataPortal.Fetch<ForumNewsBOList>(new FilterCriteriaForumNews_ByID(id));
        }
        #endregion
        #region Authorization Rules

        protected static void AddObjectAuthorizationRules()
		{
			//TODO: Define object-level authorization rules of ForumNewsBOList
			//AuthorizationRules.AllowCreate(typeof(ForumNewsBOList), "ForumNewsBOListCreateGroup"));
			//AuthorizationRules.AllowEdit(typeof(ForumNewsBOList), "ForumNewsBOListEditGroup"));
			//AuthorizationRules.AllowDelete(typeof(ForumNewsBOList), "ForumNewsBOListDeleteGroup"));
		}
		#endregion //Authorization Rules
        #region khai bao bien
        private class FilterCriteriaForumNews_ByID
        {
            public int idcategory;
            public FilterCriteriaForumNews_ByID(int id)
            {
                this.idcategory = id;
            }
        }
        #endregion
        #region Trang Danh muc

        #region Ham kiem tra tin con ton tai trong sub danh muc topic forum can xoa
        public static bool CheckSubCategoryInNewForum(int subcateID)
        {
            int count = 0;
            using (var mgr = ContextManager<HousingInfoDataContext>
                    .GetManager(Database.HousingInfoConnection))
            {
                count = mgr.DataContext.ForumNews.Count(n => n.SubTopicForumID == subcateID);
            }

            return (count > 0);
        }
        #endregion // end kiem tra khi xoa sub danh muc

        #region Ham dem tong so luong bai viet theo chu de
        public static int CountTotalForumNews_BySubID(int subcateID)
        {
            int count = 0;
            using (var mgr = ContextManager<HousingInfoDataContext>
                    .GetManager(Database.HousingInfoConnection))
            {
                count = mgr.DataContext.ForumNews.Count(n => n.SubTopicForumID == subcateID);
            }

            return count;
        }
        #endregion // end kiem tra so luong tin

        #endregion //Trang Danh muc
        #region truy cap du lieu cua ham lay tat ca danh sach tin dien dan(ForumNews)
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch()
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    from row in mgr.DataContext.ForumNews
                    orderby row.CreatedDate descending
                    select ForumNewsBO.GetForumNewsBO(row.ForumNewID)
                );

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion

        #region Ham truy cap du lieu lay id tu bang danh muc
        private void DataPortal_Fetch(FilterCriteriaForumNews_ByID cretia)
        {
            bool cancel = false;
            if (cancel) return;
            using (var mgr= ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                this.AddRange(
                        from row in mgr.DataContext.ForumNews
                        where row.SubTopicForumID == cretia.idcategory
                        select ForumNewsBO.GetForumNewsBO(row.ForumNewID)
                    );
                RaiseListChangedEvents=true;
            }
        }
        #endregion

        #region Ham xoa tat ca cac tin dien dan theo chu de
        public static void DeleteAllForumsNews_BySubcateID(int subCategoryID)
        {
            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                var result = from row in mgr.DataContext.ForumNews
                             where row.SubTopicForumID == subCategoryID
                             select row;
                foreach (var item in result)
                {                    
                    var resultcomment = from rowcomment in mgr.DataContext.ForumCommemts
                                        where rowcomment.ForumNewID == item.ForumNewID
                                        select rowcomment;
                    foreach (var itemcomment in resultcomment)
                    {
                        ForumCommentBO.DeleteForumCommentBO(itemcomment.ForumCommentID);
                    }
                    ForumNewsBO.DeleteForumNewsBO(item.ForumNewID);
                }
            }

        }
        #endregion

        #region Trang Chu Dien Dan Lay so luong tin cho combobox
        public static ForumNewsBOList GetForumNewsBOList_ByNumber(int number)
        {
            return DataPortal.Fetch<ForumNewsBOList>(new FilterCriteriaAllForumNews_ByNumber(number));
        }
        private class FilterCriteriaAllForumNews_ByNumber
        {
            public int number;
            public FilterCriteriaAllForumNews_ByNumber(int num)
            {
                this.number = num;
            }
        }
        private void DataPortal_Fetch(FilterCriteriaAllForumNews_ByNumber cretia)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    (from row in mgr.DataContext.ForumNews
                    orderby row.CreatedDate descending
                    select ForumNewsBO.GetForumNewsBO(row.ForumNewID)).Take(cretia.number)
                );
                RaiseListChangedEvents = true;
            }//using
        }
        #endregion

        #region Trang Dien Dan
        #region Ham lay 1 tin moi nhat theo chu de
        public static ForumNewsBOList GetOneForumNews_ByIDSubTopic(int subTopicID)
        {
            return DataPortal.Fetch<ForumNewsBOList>(new FilterCriteria_OneForumNews_BySubID(subTopicID));
        }
        #endregion

        #region khai bao bien
        private class FilterCriteria_OneForumNews_BySubID
        {
            public int subID;
            public FilterCriteria_OneForumNews_BySubID(int id)
            {
                this.subID = id;
            }
        }
        #endregion
        #region Truy Cap Du Lieu
        private void DataPortal_Fetch(FilterCriteria_OneForumNews_BySubID cretia)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    (from row in mgr.DataContext.ForumNews
                     where row.SubTopicForumID == cretia.subID
                     orderby row.CreatedDate descending
                     select ForumNewsBO.GetForumNewsBO(row.ForumNewID)).Take(1)
                );
                RaiseListChangedEvents = true;
            }//using
        }
        #endregion

        #region Ham lay 5 tin moi nhat theo chu de
        public static ForumNewsBOList GetFiveForumNews_ByIDSubTopic(int subTopicID)
        {
            return DataPortal.Fetch<ForumNewsBOList>(new FilterCriteria_FiveForumNews_BySubID(subTopicID));
        }
        #region khai bao bien
        private class FilterCriteria_FiveForumNews_BySubID
        {
            public int subID;
            public FilterCriteria_FiveForumNews_BySubID(int id)
            {
                this.subID = id;
            }
        }
        #endregion
        #region Truy Cap Du Lieu
        private void DataPortal_Fetch(FilterCriteria_FiveForumNews_BySubID cretia)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    (from row in mgr.DataContext.ForumNews
                     where row.SubTopicForumID == cretia.subID
                     orderby row.CreatedDate descending
                     select ForumNewsBO.GetForumNewsBO(row.ForumNewID)).Take(5)
                );
                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #endregion
        #region Ham lay tat ca cac tin theo chu de
        public static ForumNewsBOList GetAllForumNews_ByIDSubTopic(int subTopicID)
        {
            return DataPortal.Fetch<ForumNewsBOList>(new FilterCriteria_AllForumNews_BySubID(subTopicID));
        }
        #endregion
        #region khai bao bien
        private class FilterCriteria_AllForumNews_BySubID
        {
            public int subID;
            public FilterCriteria_AllForumNews_BySubID(int id)
            {
                this.subID = id;
            }
        }
        #endregion
        #region Truy Cap Du Lieu
        private void DataPortal_Fetch(FilterCriteria_AllForumNews_BySubID cretia)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    (from row in mgr.DataContext.ForumNews
                     where row.SubTopicForumID == cretia.subID
                     orderby row.CreatedDate descending
                     select ForumNewsBO.GetForumNewsBO(row.ForumNewID))
                );
                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #endregion

        #region Ket qua tim kiem - theo tu khoa nhap vao
        public static ForumNewsBOList Ketquatimkiem_theotukhoa(string tukhoa, int sotrang, int soluong)
        {
            return DataPortal.Fetch<ForumNewsBOList>(new FilterCriteriaKetquatimkiem_theotukhoa(tukhoa, sotrang, soluong));
        }
        private class FilterCriteriaKetquatimkiem_theotukhoa
        {
            public string tukhoa;
            public int sotrang;
            public int soluong;
            public FilterCriteriaKetquatimkiem_theotukhoa(string key, int st, int sl)
            {
                this.tukhoa = key;
                this.sotrang = st;
                this.soluong = sl;
            }
        }
        private void DataPortal_Fetch(FilterCriteriaKetquatimkiem_theotukhoa cretia)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                var result = from row in mgr.DataContext.ForumNews
                             where row.Title.Contains(cretia.tukhoa)
                             select row;
                if (cretia.sotrang == -1 && cretia.soluong == -1)
                {
                    this.AddRange(
                        from row in result
                         orderby row.CreatedDate descending
                         select ForumNewsBO.GetForumNewsBO(row.ForumNewID)
                    );
                }
                else
                {
                    this.AddRange(
                        (from row in result
                         orderby row.CreatedDate descending
                         select ForumNewsBO.GetForumNewsBO(row.ForumNewID)).Skip((cretia.sotrang - 1)*cretia.soluong).Take(cretia.soluong)
                    );
                }
                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
    }
}